package com.zhongkun.datahome.web.app;

import com.zhongkun.datahome.common.core.domain.R;
import com.zhongkun.datahome.common.core.domain.model.LoginUser;
import com.zhongkun.datahome.common.utils.SecurityUtils;
import com.zhongkun.datahome.service.domain.CustomerBase;
import com.zhongkun.datahome.service.repository.CustomerBaseRepository;
import com.zhongkun.datahome.service.res.house.CustomerHouseAppVo;
import com.zhongkun.datahome.service.res.house.CustomerHouseSelectVo;
import com.zhongkun.datahome.service.service.ICustomerFamilyService;
import com.zhongkun.datahome.service.service.ICustomerHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author: lifengsheng
 * @Date: 2025/5/7 11:42
 * @Desc
 */
@Api("小程序接口")
@RestController
@RequestMapping("/app/customerHouse")
public class CustomerHouseAppController {
    @Resource
    private ICustomerHouseService customerHouseService;

    @Resource
    private ICustomerFamilyService customerFamilyService;

    @Resource
    private CustomerBaseRepository customerBaseRepository;

    @ApiOperation("获取当前用户信息")
    @GetMapping("/getUserInfo")
    public R<LoginUser> getUserInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (Objects.isNull(loginUser) || Objects.isNull(loginUser.getUser())) {
            return R.fail("用户未登陆");
        }

        boolean customerFlag = customerBaseRepository.countByTenantUserId(loginUser.getTenantUserId()) > 0;
        loginUser.getUser().setCustomer(customerFlag);
        return R.ok(loginUser);
    }

    @ApiOperation("查询房产下拉列表")
    @GetMapping("/selectHouseInfo")
    public R<List<CustomerHouseSelectVo>> selectHouseInfo(@RequestParam(required = false) String roomNum) {
        return R.ok(customerHouseService.selectHouseInfo(roomNum));
    }

    @ApiOperation("查询房产信息列表")
    @GetMapping("/listHouseInfo")
    public R<List<CustomerHouseAppVo>> listHouseInfo(String roomNum, boolean employeeFlag) {
        return R.ok(customerHouseService.listHouseInfoForApp(roomNum, employeeFlag));
    }

    @ApiOperation("查询房产信息")
    @GetMapping("/getHouseInfo")
    public R<CustomerHouseAppVo> getHouseInfo(Long houseId) {
        return R.ok(customerHouseService.getHouseInfoForApp(houseId));
    }

    @ApiOperation("查询家庭成员列表")
    @GetMapping("/listMembers")
    public R<List<CustomerBase>> listMembers() {
        return R.ok(customerFamilyService.listMembersForApp());
    }
}
